package datamanagement;

import healthsolution.wk.R;

import datamanagement.MedicalUser;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;


public class MedicalUser extends Activity  implements Runnable
{
	private static final String TAG = "ParticularUserActivity";
	
	
	public Thread currentThread = new Thread(this);
	public Handler threadHandler = new Handler();	
	//private Handler handler = new Handler();
	
	public MedicalUser()
	{

	}
		
	public void run() {
		// TODO Auto-generated method stub
		
		Log.i(TAG, "[run] Run");									//DEBUG
		try
		{
			threadHandler.postDelayed(this,1000);			
			//voStartListenerButtons();						
			//threadHandler.sendEmptyMessage(0);
						
		}catch(Exception Ex)
		{
			Log.e(TAG, "[run] Error: " +Ex.getMessage());									//DEBUG
		}
		    
	}
	
	
	
    
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		Log.i(TAG, "[onCreate] onCreate");		
		 super.onCreate(savedInstanceState);	
		 
		 boSetView();
		 
		 try
		 {
			 voStartListenerButtons();
			
		 }catch(Exception ex)
		 {
			 Log.e(TAG, "[onCreate] Exception Start Listener: "+ex.getMessage());			 
			 Log.e(TAG, "[onCreate] Exception Start Listener: "+ex.getCause());
			 Log.e(TAG, "[onCreate] Exception Start Listener: "+ex.getLocalizedMessage());
		 }
		 try
		 {
			 currentThread.start();
			 threadHandler.postDelayed(this,1000);
			 	 
		 }catch(Exception ex)
		 {
			 Log.e(TAG, "[onCreate] Exception Thread: "+ex.getMessage());			 
		 }		 
	}
		
	public void voStartListenerButtons()
	{
		try
		{
        	ImageButton btLeerDatos = (ImageButton) findViewById(R.id.iconBtnLeerDatos);    		
        	ImageButton btOrdenMedica = (ImageButton) findViewById(R.id.iconBtnOrdenMed);
    		
    		btLeerDatos.setOnClickListener(new OnClickListener()
    	    {     
    			public void onClick(View v) 
    			{				
    				Log.i(TAG,"[voStartListenerButtons] Dentro OnClick LeerDatos");
    				
    			    
    				 Intent IntWelcome = null;
					    Log.i(TAG,"[onCreate] Voy a empezar activity LeerDatos. ");
					    
					    try
					    {
					    	IntWelcome=new Intent(getApplicationContext(),LeerDatos.class);
					    }catch(Exception Ex)
					    {
					    	Log.e(TAG,"[onCreate] Exception LeerDatos Intent: "+Ex);	
					    }
					    try
						{
					    	startActivity(IntWelcome);
						}catch(Exception ex)
						{
							Log.i(TAG,"[onCreate] Exception LeerDatos Start: "+ex);	
						}
					    
    			    			
    			}
    	    });
    		
    	    		
    		btOrdenMedica.setOnClickListener(new OnClickListener()
    	    {     
    			public void onClick(View v) 
    			{				
    				Log.i(TAG,"[voStartListenerButtons] Dentro OnClick Orden Medica");
    				
    			    	
    					
    			    			
    			}
    	    });

		}catch(Exception ex)
		{
			Log.e(TAG, "[voStartListenerButtons] Exception Start Listener Buttons: "+ex.getMessage());
			Log.e(TAG, "[voStartListenerButtons] Exception Start Listener Buttons: "+ex.getCause());
			Log.e(TAG, "[voStartListenerButtons] Exception Start Listener Buttons: "+ex.getStackTrace());
			Log.e(TAG, "[voStartListenerButtons] Exception Start Listener Buttons: "+ex);
		}
		
    		        	
	}
	
	///////////////////////////////////////////////////// Set View //////////////////////////////
	public boolean boSetView()
	{
		// SharedPreferences settings = getSharedPreferences("HS",MODE_PRIVATE);
     	Log.i(TAG, "[boSetView] Set ContentView");
		setContentView(R.layout.pugral);
		
		return true;
	}

	///////////////////////////////////////////onRestart///////////////////////////////////////////////////////////
	 @Override
	 protected void onRestart() {
	     super.onRestart();  // Always call the superclass method first
	     
	 	Intent myIntent=null;
    	try
       	{
    		//myIntent = new Intent(cont, HTService.class);
    		
    		 myIntent=new Intent(this.getApplicationContext(),MedicalUser.class);
	    		
	       	}catch(Exception ex)
	         {
	        	 Log.e("PositionCYSActivity","[onCreate] ERROR I EX: "+ex);		        
	         }	    	
	       	try
	       	{
	       		startActivity(myIntent);
	         }catch(Exception ex)
	         {
	        	 Log.e("PositionCYSActivity","[onCreate] ERROR A EX: "+ex);
	         }
	     // Activity being restarted from stopped state    
	 }
	 
		
	
	
	///////////////////////////////////////////////////////////////////////////////////////////
	public IBinder onBind(Intent arg0)
	{
	    return null;
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////
	// PARA QUE NO PUEDA REGRESAR A LA PANTALLA ANTERIOR
	@Override
	public void onBackPressed() {
		Log.i(TAG, "[onBackPressed] onBackPressed");
	    voMinimizarApp();
	}
	
	public void voMinimizarApp()
	{
		try
		{
			Intent startMain = new Intent(Intent.ACTION_MAIN);
	    	startMain.addCategory(Intent.CATEGORY_HOME);
	    	startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    	startActivity(startMain);
	    	
		}catch(Exception ex)
		{
			Log.i(TAG, "[voMinimizarApp] Exception: "+ex);
		}
		
	}
	
}
